<?php

declare(strict_types=1);

namespace App\Controller\admin;

use App\Constants\ErrorCode;
use App\Controller\AbstractController;
use App\Model\admin\SystemPermission;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\RequestMapping;
use Hyperf\Di\Annotation\Inject;
use Psr\Http\Message\ResponseInterface;

/**
 * 系统权限
 * Class PermissionController
 * @package App\Controller\admin
 * @Controller(prefix="admin")
 */
class SystemPermissionController extends AbstractController
{
    /**
     * @Inject
     * @var SystemPermission
     */
    protected $model;

    /**
     * 验证规则
     * @var string[]
     */
    private $rule = [
        'title'=>'required',
        'pid'=>'required',
        'route' => 'required',
        'component' => 'required'
    ];

    /**
     * 验证错误信息
     * @var string[]
     */
    private $message = [
        'title.required' => '名称不能为空',
        'route.required' => '前端路由不能为空',
        'component.required' => '前端组件不能为空',
        'pid.required'=>'上级权限不能为空',
    ];

    /**
     * 列表
     * @RequestMapping(path="permission",methods="get")
     * @return ResponseInterface
     */
    public function index(): ResponseInterface
    {
        $list = $this->model->query()->orderBy('sort', 'desc')->get(['id', 'pid', 'title', 'icon', 'status']);
        $list = buildTree($list);
        return $this->success($list);
    }

    /**
     * 保存
     * @RequestMapping(path="permission",methods="post")
     */
    public function store(): ResponseInterface
    {
        $data = $this->request->all();

        $validator  = $this->validationFactory->make($data,$this->rule,$this->message);

        if ($validator->fails()){
            $errorMessage = $validator->errors()->first();
            return $this->fail(ErrorCode::BUSINESS_FAIL,$errorMessage);
        }
        if ($this->model->create($data))
            return $this->success();

        return $this->fail(ErrorCode::BUSINESS_FAIL, '保存失败');
    }

    /**
     * 详情
     * @RequestMapping(path="permission/{id}",methods="get")
     */
    public function show($id): ResponseInterface
    {
        return $this->success($this->model->query()->find($id));
    }

    /**
     * 更新
     * @RequestMapping(path="permission",methods="put")
     */
    public function update(): ResponseInterface
    {
        $data = $this->request->all();

        $validator  = $this->validationFactory->make($data,$this->rule,$this->message);

        if ($validator->fails()){
            $errorMessage = $validator->errors()->first();
            return $this->fail(ErrorCode::BUSINESS_FAIL,$errorMessage);
        }

        $info = $this->model->query()->find($data['id']);

        if (!$info) return $this->fail(ErrorCode::BUSINESS_FAIL,'不存在');

        if ($info->update($data)) return $this->success();

        return $this->fail(ErrorCode::BUSINESS_FAIL, '保存失败');
    }

    /**
     * 删除
     * @RequestMapping(path="permission/{id}",methods="delete")
     */
    public function destroy($id): ResponseInterface
    {
        if ($this->model->where('id', $id)->delete()) {
            return $this->success();
        }
        return $this->fail(ErrorCode::BUSINESS_FAIL,'删除失败');
    }
}
